package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.region.Region;
import com.ecoolex.framework.modularize.api.service.region.RegionService;
import com.ecoolex.framework.modularize.api.service.storage.*;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication;
import com.ecoolex.ying.space.entity.UserInfoExt;
import com.ecoolex.ying.space.ws.UserIdentityAuthenticationService;
import com.ecoolex.ying.space.ws.UserInfoExtService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;


@GraphQL2Module(type = GraphQL2Module.Type.User, description = "用户资料")
public class UserInfoExtGraphql {

    @Reference
    UserInfoExtService userInfoExtService;
    @Reference
    private UserIdentityAuthenticationService userIdentityAuthenticationService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private RegionService regionService;

    @TypeField(type = UserInfoExt.class, field = "region", description = "地区")
    public Region userRegion(@Source UserInfoExt source) {
        return regionService.get(source.getRegionId());
    }

    @TypeField(type = ConsumerMember.class, field = "ext", description = "用户扩展信息")
    public UserInfoExt ext(@Source ConsumerMember source) {
        return userInfoExtService.getByUserId(source.getUserId());
    }

    @TypeField(type = ConsumerMember.class, field = "identityAuthentication", description = "用户身份认证")
    public UserIdentityAuthentication identityAuthentication(@Source ConsumerMember source) {
        return userIdentityAuthenticationService.getByUserId(source.getUserId());
    }

    @TypeField(type = UserInfoExt.class, field = "coverImage", description = "用户封面图")
    public StorageInfo coverImage(@Source UserInfoExt source) {
        return storageService.get(source.getCoverImage());
    }

    @QueryField(field = "selfUserInfo", description = "用户自己的资料")
    public ConsumerMember selfUserInfo(ConsumerMemberAuthority user) {
        user.assertLogin();
        return userInfoExtService.getConsumerMemberByUserId(user.userId());
    }

    @QueryField(field = "otherUserInfo", description = "他人用户的资料")
    public ConsumerMember otherUserInfo(Integer userId) {
        return userInfoExtService.getConsumerMemberByUserId(userId);
    }

    @MutationField(field = "uploadUserCoverImage", description = "用户资料-上传用户封面图片")
    public UserInfoExt uploadUserCoverImage(@NonNull Upload request, ConsumerMemberAuthority user) {
        user.assertLogin();
        UploadSource uploadSource = new UploadSource("UserInfoExt", "Image");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        StorageInfo storageInfo = storageService.upload(request, uploadSource, uploadLimit);

        UserInfoExt record = userInfoExtService.getByUserId(user.userId());
        record.setCoverImage(storageInfo.getStorageId());
        return userInfoExtService.editUserInfoExt(record, user.userName());
    }

    @MutationField(field = "editUserInfoIntroduce", description = "用户资料-设置简介")
    public UserInfoExt editIntroduce(@NonNull String content, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserInfoExt record = userInfoExtService.getByUserId(user.userId());
        record.setIntroduce(content);
        return userInfoExtService.editUserInfoExt(record, user.userName());
    }

    @MutationField(field = "editUserInfoRegionId", description = "用户资料-设置地区")
    public UserInfoExt editRegioinId(@NonNull Integer regionId, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserInfoExt record = userInfoExtService.getByUserId(user.userId());
        record.setRegionId(regionId);
        return userInfoExtService.editUserInfoExt(record, user.userName());
    }

    @MutationField(field = "editUserInfoRegionDetail", description = "用户资料-设置地区详情")
    public UserInfoExt editUserInfoRegionDetail(@NonNull String regionDetail, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserInfoExt record = userInfoExtService.getByUserId(user.userId());
        record.setRegionDetail(regionDetail);
        return userInfoExtService.editUserInfoExt(record, user.userName());
    }

}
